﻿using LxBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LxCore
{
    public class DensityFieldKeeper : IZoneField<double>
    {
        protected FvZone Zone;
        protected IField<double> Density;
        protected ArrayScalar.BaseCell Keeper;
        public void CheckCreate(FvZone zone, IField<double> densityField)
        {
            this.Zone = zone;
            this.Density = densityField;
            if (densityField.IsParameter(out double _))
            {
                Keeper = null;
            }
            else
            {
                CheckCreator.Instance(ref Keeper);
                Keeper.CheckCreate(zone);
            }
        }
        public bool IsParameter(out double value)
        {
            return Density.IsParameter(out value);
        }

        public void UpdateDensity(ThreadInfo threadInfo)
        {
            if (Density.IsParameter(out double constden)) return;
            var InnArray = Keeper.InnArray;
            var BouArray = Keeper.BouArray;
            {

                (int i1, int endp1) = Zone.Inner.CellIdx.Split(threadInfo);
                var v = Density.GetCellVarArr(Zone.Inner);
                for (; i1 < endp1; i1++)
                {
                    InnArray[i1] = v.GetCellVar(i1);
                }
            }
            foreach (var b in Zone.Bounds)
            {
                (int i1, int endp1) = b.CellIdx.Split(threadInfo);
                var v = Density.GetCellVarArr(b);
                for (; i1 < endp1; i1++)
                {
                    BouArray[i1] = v.GetCellVar(i1);
                }
            }
        }
        //public override ICellVarArr<double> GetCellVarArr(IIndexHost indexHost)
        //{
        //    if (Density.IsParameter(out double _))
        //    {
        //        return Density.GetCellVarArr(indexHost);
        //    }
        //    else
        //    {
        //        return Keeper.GetCellVarArr(indexHost);
        //    }

        //}
        //public override IFaceVarArr<double> GetFaceVarArr(IIndexHost indexHost)
        //{
        //    if (Density.IsParameter(out double _))
        //    {
        //        return Density.GetFaceVarArr(indexHost);
        //    }
        //    else
        //    {
        //        return Keeper.GetFaceVarArr(indexHost);
        //    }
        //}

        public ICellVarArr<double>? GetInnCellVarArr()
        {
            if (Density == null) return null;
            return Density.IsParameter(out double c) ? Density.GetCellVarArr(null) : Keeper.GetInnCellVarArr();
        }

        public ICellVarArr<double>? GetBouCellVarArr()
        {
            if (Density == null) return null;
            return Density.IsParameter(out double c) ? Density.GetCellVarArr(null) : Keeper.GetBouCellVarArr();
        }

        public IFaceVarArr<double>? GetInnFaceVarArr()
        {
            if (Density == null) return null;
            return Density.IsParameter(out double c) ? Density.GetFaceVarArr(null) : Keeper.GetInnFaceVarArr();
        }

        public IFaceVarArr<double>? GetBouFaceVarArr()
        {
            if (Density == null) return null;
            return Density.IsParameter(out double c) ? Density.GetFaceVarArr(null) : Keeper.GetBouFaceVarArr();
        }
    }
}
